package TextEditor;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JTextArea;
/**
 * @deprecated
 * @see TextEditor.facade package
 * @author Yij_Zhan
  */
public class FileCutPaste {
	
	private JTextArea textArea;

	//@ZHYJ: SYSTEM CLIPBORAD FOR DATA TRANSFER
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Clipboard clipBoard = toolkit.getSystemClipboard();
	
	public FileCutPaste(JTextArea textArea){  	
		this.textArea = textArea;  	
	}

	public void copy(){
		//@ZHYJ:
		textArea.requestFocusInWindow();
		String copyString = textArea.getSelectedText();
		StringSelection selection = new StringSelection(copyString);
		clipBoard.setContents(selection,null);		
	}
	
	public void cut(){
		textArea.requestFocusInWindow();
		String cutString = textArea.getSelectedText();
		StringSelection selection = new StringSelection(cutString);
		clipBoard.setContents(selection,null);

		textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());		
	}
	
	public void paste(){
		//@ZHYJ:
		textArea.requestFocusInWindow();
		Transferable contents = clipBoard.getContents(this);
		
		if(contents == null)  return;
		String pasteString = "";

		try {
			pasteString = (String)contents.getTransferData(DataFlavor.stringFlavor);
		} catch (UnsupportedFlavorException e1) {
			
			e1.printStackTrace();
		} catch (IOException e1) {
			
			e1.printStackTrace();
		}

		textArea.replaceRange(pasteString,textArea.getSelectionStart(),textArea.getSelectionEnd());
	
	}
	
	public void additiveCut(){
		//@ZHYJ:
		textArea.requestFocusInWindow();

		Transferable contents = clipBoard.getContents(this);
		String clipboardString = "";

		try {
			clipboardString = (String)contents.getTransferData(DataFlavor.stringFlavor);
		} catch (UnsupportedFlavorException e1) {		
			e1.printStackTrace();
		} catch (IOException e1) {		
			e1.printStackTrace();
		}
		String newCutString = textArea.getSelectedText();
		String addCutString = clipboardString.concat(newCutString);

		StringSelection selection = new StringSelection(addCutString);
		clipBoard.setContents(selection,null);
		textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
		
	}
	
	public void additiveCopy(){
		textArea.requestFocusInWindow();

		Transferable contents = clipBoard.getContents(this);
		String clipboardString = "";

		try {
			clipboardString = (String)contents.getTransferData(DataFlavor.stringFlavor);
		} catch (UnsupportedFlavorException e1) {		
			e1.printStackTrace();
		} catch (IOException e1) {		
			e1.printStackTrace();
		}
		String copyString = textArea.getSelectedText();

		String addCopyString = clipboardString.concat(copyString);
		StringSelection selection = new StringSelection(addCopyString);

		clipBoard.setContents(selection,null);		
	}

	
}
